package com.oocl.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.oocl.dao.CustFavsDao;
import com.oocl.dao.CustomerDao;
import com.oocl.dao.impl.CustFavsDaoImpl;
import com.oocl.dao.impl.CustomerDaoImpl;
import com.oocl.formV1.util.FavoriteDB;
import com.oocl.pojo.Customer;
import com.oocl.pojo.Favorite;
import com.oocl.service.CustomerService;

public class CustomerServiceImpl implements CustomerService{

	private CustomerDao customerDao;
	private CustFavsDao custFavsDao;
	
	public CustomerServiceImpl() {
		customerDao = new CustomerDaoImpl();
		custFavsDao = new CustFavsDaoImpl();
	}

	@Override
	public int addCustomer(Customer customer) {
		
		customerDao.addCustomer(customer);
		List<Favorite> favs = customer.getFavorites();

		List<Integer> favs_ids = new ArrayList<Integer>();
		for(int i=0; i<favs.size();i++){
			System.out.println("---------------"+favs.get(i));
			favs_ids.add(FavoriteDB.getFavorites().get(favs.get(i).getName()));  //注意+1
		}
		int lastCustId = customerDao.getLastId();
		
		return custFavsDao.addCustFavs(lastCustId, favs_ids);
	}

	@Override
	public List<Customer> findAllCustomers() {
		return customerDao.findAllCustomers();
	}

	@Override
	public boolean verifyNameFormat(String name) {
		String nameRegex = "^(([\u4e00-\u9fa5]{2,8})|([a-zA-Z]{2,16}))$";
		Pattern p = Pattern.compile(nameRegex);
		Matcher m = p.matcher(name);
		if(m.matches()){
			return true;
		}
		return false;
	}

	@Override
	public boolean delByCustId(int cust_id) {
		custFavsDao.delByCustId(cust_id);
		return customerDao.delByCustId(cust_id);
	}

}
